﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Testicide.Models;
using Testicide.DAL;

namespace Testicide.Controllers
{
    public class ModuleController : Controller
    {
        private TesticideContext db = new TesticideContext();

        //
        // GET: /Module/

        public ActionResult Index(int? id)
        {
            if (id == null && TempData["pid"] != null) 
            {
                int proID = (int)TempData["pid"];
                var module = from m in db.Module where m.ProjectID == proID select m;
                ViewBag.Message = "yes";
                ViewBag.projectName = db.Project.Find(proID).Name;
                ViewBag.projectID = proID;
                return View(module.ToList());
            }
            else if(id != null)
            { 
                var module = from m in db.Module where m.ProjectID == id select m;
                ViewBag.Message = "yes";
                ViewBag.projectName = db.Project.Find(id).Name;
                ViewBag.projectID = id;
                return View(module.ToList());
            }
            ViewBag.Message = "no";
            return View(db.Module.ToList());
        }

        //
        // GET: /Module/Details/5

        public ActionResult Details(int id = 0)
        {
            Module module = db.Module.Find(id);
            if (module == null)
            {
                return HttpNotFound();
            }
            TempData["pid"] = module.ProjectID;
                
            return View(module);
        }

        //
        // GET: /Module/Create
        public ActionResult Create(int id=0)
        {
            Module module = new Module();
            module.ProjectID = id;
            TempData["pid"] = module.ProjectID;
            return View();
        }

        //
        // POST: /Module/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Module module)
        {
            module.Date = DateTime.Now;
            int proID = (int) TempData["pid"];
            module.ProjectID = proID;

            if (ModelState.IsValid)
            {
                db.Module.Add(module);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(module);
        }

        //
        // GET: /Module/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Module module = db.Module.Find(id);
            if (module == null)
            {
                return HttpNotFound();
            }
            TempData["pid"] = module.ProjectID;
            ViewBag.ProjectID = new SelectList(db.Project, "ID", "Name", module.ProjectID);
            return View(module);
        }

        //
        // POST: /Module/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Module module)
        {
            if (ModelState.IsValid)
            {
                db.Entry(module).State = EntityState.Modified;
                db.SaveChanges();
                TempData["pid"] = module.ProjectID;
                return RedirectToAction("Index");
            }
            ViewBag.ProjectID = new SelectList(db.Project, "ID", "Name", module.ProjectID);
            return View(module);
        }

        //
        // GET: /Module/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Module module = db.Module.Find(id);
            if (module == null)
            {
                return HttpNotFound();
            }
            TempData["pid"] = module.ProjectID;
            return View(module);
        }

        //
        // POST: /Module/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Module module = db.Module.Find(id);
            List<Feature> features = module.Features.ToList();
            foreach(var item in features)
            {
                List<Scenario> scenarios = item.Scenarios.ToList();
                foreach(var item2 in scenarios)
                {
                    db.Scenario.Remove(item2);
                }
                db.Feature.Remove(item);
            }
            db.Module.Remove(module);
            db.SaveChanges();
            TempData["pid"] = module.ProjectID;
            return RedirectToAction("Index");
        }

        //
        //Search method
        //
        public ActionResult Search(string searchString)
        {
            var modules = from p in db.Module
                           select p;

            if (!String.IsNullOrEmpty(searchString))
            {
                modules = modules.Where(p => p.Name.ToUpper().Contains(searchString.ToUpper()));
            }
            if (modules.Count() == 0)
            {
                ViewBag.Message = "The Module You are searching for doesn't exist";
            }
            else
            {
                ViewBag.Message = "";
            }
            return View(modules);
        }
        //
        //AutoComplete
        //
        public JsonResult AutoComplete(string term)
        {
            var result = db.Module.Where(p => p.Name.ToLower().Contains(term.ToLower()))
                .Select(p => p.Name).ToList();

            return this.Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult AddEditRecord(int? id, int proID)
        {
            if (Request.IsAjaxRequest())
            {
                if (id != null)
                {
                    ViewBag.IsUpdate = true;
                    Module module = db.Module.Where(m => m.ID == id).FirstOrDefault();
                    return PartialView("_ModuleData", module);
                }
                ViewBag.IsUpdate = false;
                TempData["pid"] = proID;
                return PartialView("_ModuleData");
            }
            else
            {
                if (id != null)
                {
                    ViewBag.IsUpdate = true;
                    Module module = db.Module.Where(m => m.ID == id).FirstOrDefault();
                    return PartialView("ModuleData", module);
                }
                ViewBag.IsUpdate = false;
                TempData["pid"] = proID;
                return View("ModuleData");
            }
        }

        [HttpPost]
        public ActionResult AddEditRecord(Module module, string cmd)
        {
            if (ModelState.IsValid)
            {
                if (cmd == "Save")
                {
                    module.Date = DateTime.Now;
                    module.ProjectID = (int)TempData["pid"];
                    db.Module.Add(module);
                    db.SaveChanges();
                    TempData["pid"] = module.ProjectID;
                    return RedirectToAction("Index");
                }
                else
                {
                    Module mod = db.Module.Where(m => m.ID == module.ID).FirstOrDefault();
                    if (mod != null)
                    {
                        mod.Name = module.Name;
                        mod.Date = module.Date;
                        db.SaveChanges();
                    }
                    TempData["pid"] = mod.ProjectID;
                    return RedirectToAction("Index");                    
                }
            }
            TempData["pid"] = module.ProjectID;
            return RedirectToAction("Index");
        }

        public ActionResult DeleteRecord(int id)
        {
            Module module = db.Module.Find(id);
            List<Feature> features = module.Features.ToList();
            foreach (var item in features)
            {
                List<Scenario> scenarios = item.Scenarios.ToList();
                foreach (var item2 in scenarios)
                {
                    db.Scenario.Remove(item2);
                }
                db.Feature.Remove(item);
            }
            db.Module.Remove(module);
            db.SaveChanges();

            TempData["pid"] = module.ProjectID;
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}